<template>
  <div class="app-container">
    <div class="query-panel">
      <div class="query-panel-filter">
        <div class="query-form">
          <el-form
            :model="queryParams"
            ref="queryForm"
            :inline="true"
            v-show="showSearch"
            label-width="120px"
          >
            <el-form-item label="作业票类型">
              <el-select
                v-model="queryParams.businesstype"
                clearable
                size="small"
              >
                <el-option
                  v-for="item in selectworktypelist"
                  :key="item.label"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="作业票编号" prop="content">
              <el-input
                v-model="queryParams.content"
                placeholder="请输入作业票编号"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-form>
        </div>
        <div class="query-btn"></div>
      </div>
      <div class="query-search">
        <div class="query-search-btn">
          <div class="query-search-btn">
            <el-button
              class="c-btn-reset"
              icon="el-icon-query-reset"
              size="mini"
              @click="resetQuery"
              >重置</el-button
            >
            <el-button
              class="c-btn-search"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
          </div>
        </div>
      </div>
    </div>

    <el-table
      v-loading="loading"
      :data="operlogList"
      @selection-change="handleSelectionChange"
      stripe
    >
      <el-table-column label="作业票类型类型" align="center">
        <template slot-scope="scope">
          {{ getworkname(scope.row.businesstype) }}
        </template>
      </el-table-column>
      <el-table-column label="作业票编号" align="center" prop="content" />
      <el-table-column label="操作" align="center" prop="title" />
      <el-table-column
        label="删除时间"
        align="center"
        prop="createts"
        width="180"
      ></el-table-column>
      <el-table-column label="删除人" align="center">
        <template slot-scope="scope">
          <span>{{
            scope.row.user != null ? scope.row.user.userFullName : ""
          }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改操作日志对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="业务类型" prop="businesstype">
          <el-select v-model="form.businesstype" placeholder="请选择业务类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="createts">
          <el-date-picker
            clearable
            size="small"
            v-model="form.createts"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="createdby">
          <el-input v-model="form.createdby" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="公司id" prop="companyid">
          <el-input v-model="form.companyid" placeholder="请输入公司id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOperlog,
  getOperlog,
  delOperlog,
  addOperlog,
  updateOperlog,
  exportOperlog,
} from "@/api/basic/operlog";
import { selectworktypelist } from "@/utils/workenums";
export default {
  name: "Operlog",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 操作日志表格数据
      operlogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        orderByColumn: "createts",
        isAsc: "desc",
        pageNum: 1,
        pageSize: 10,
        title: null,
        content: null,
        businesstype: null,
        createts: null,
        createdby: null,
        companyid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
      },
      selectworktypelist: selectworktypelist(),
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询操作日志列表 */
    getList() {
      this.loading = true;
      listOperlog(this.queryParams).then((response) => {
        this.operlogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        content: null,
        businesstype: null,
        createts: null,
        createdby: null,
        companyid: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加操作日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOperlog(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改操作日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateOperlog(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOperlog(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除操作日志编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delOperlog(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有操作日志数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportOperlog(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    getworkname(value) {
      var result = "";
      for (var i = 0; i < this.selectworktypelist.length; i++) {
        if (this.selectworktypelist[i].value == value) {
          result = this.selectworktypelist[i].label;
          break;
        }
      }
      return result;
    },
  },
};
</script>
